package com.jservice.api.service.upp;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.UppClient;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.wondersgroup.framework.util.EncryptUtils;
import com.wondersgroup.upp.api.core.UPPlatformRequest;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Upp2025 - 重置服务密码
 *
 * @Author cwledit
 * @DATE 2017/6/23.
 */
@Service("upp2025")
public class Upp2025 extends BaseService implements IService {



    private String SERVER_NAME = "upp.reset.service.password";


    @Override
    public Response excute(Request request, Response response, ResponseHeader header, String method) throws Exception {
        Map paramMap =  request.getData();
        String key = paramsStringFilter(paramMap.get("key"));
        String token = paramsStringFilter(paramMap.get("token"));
        String new_pwd = paramsStringFilter(paramMap.get("new_pwd"));
        String id_no = paramsStringFilter(paramMap.get("id_no"));
        String sms_code = paramsStringFilter(paramMap.get("sms_code"));
        String sms_seq = paramsStringFilter(paramMap.get("sms_seq"));

        Map data = new HashMap();
        String encrypt_pwd = EncryptUtils.encrypt(new_pwd,publicKey,key);
//        String encrypt_pwd = CoderUtils.bytesToHex(AESUtils.encrypt(new_pwd.getBytes("GBK"),key.getBytes("GBK")));
        data.put("encrypt_type","AES");
        data.put("new_pwd",encrypt_pwd);
        data.put("token",token);
        data.put("id_no",id_no);
        data.put("id_type","01");
        data.put("auth_type","033");
        data.put("sms_code",sms_code);
        data.put("sms_seq",sms_seq);
        UPPlatformRequest uRequest = getUPRequest(SERVER_NAME,request.getHeader());
        uRequest.setBiz_content(sortParam(data));
        UppClient client = UppClient.getInstance();
        JSONObject jsonObject = client.doRequest(uRequest);
        if ("000000".equals(jsonObject.getString("code"))) {
            jsonObject.remove("code");
            return renderSUC(jsonObject,response,header);
        }else{
            String msg = jsonObject.getString("msg");
            String errMsg = "";
            if (msg.equals("[温州市民卡运营平台]提醒您:市民卡卡号验证失败")){
                errMsg = "输入有误,请核对";
            }else
            if (msg.equals("[温州市民卡运营平台]提醒您:商户或网点未授权操作当前卡类型")){
                errMsg = "该市民卡状态异常，无法进行充值！";
            }else
            if (msg.equals("[温州市民卡运营平台]提醒您:参数值不正确")){
                errMsg = "输入有误,请核对";
            }else
            if (msg.equals("[温州市民卡运营平台]提醒您:短信验证码错误")){
                errMsg = "短信验证码错误";
            }else
            if (msg.equals("市民卡网关返回异常:服务密码不等于6位")){
                errMsg = "服务密码不正确";
            }else{
                errMsg = msg;
            }
            return renderFAIL(MsgCode.Code_1009,errMsg,response,header);
        }
    }

}
